<?php
namespace Home\Controller;

use Think\Controller;

class ProductController extends BaseController
{
    //商品详情页
    public function index()
    {
        $productId = I('get.id');
        $product = M("product")
            ->where(array("id"=>$productId))
            ->find();

        if(empty($product) || $product["isDelete"] == 1 || $product["isUp"] == 2){
            $this->ajaxError("该商品不存在或未上架！");
        }

        return $this->display("goodinfo");
    }

    //商品模糊搜索
    public function seachProducts()
    {
        $kw = I('get.kw');
        if (empty($kw)) {
            $kw = 'all';
        }
        if ('all' == $kw) {
            $res = M('product')->field("id,categoryId,name,introduce,img,price")
                ->where(array(
                    "isDelete"=>2,
                    "isUp"=>1
                ))
                ->order("rand()")->limit(6)->select();
        } else {
            $res = M('product')->field("id,categoryId,name,introduce,img,price")
                ->where(array(
                    "name"=>array("like", "%$kw%"),
                    "isDelete"=>2,
                    "isUp"=>1
                ))
                ->select();
        }

        $this->ajaxSuccess($res);
    }

    /**
     * 搜索展示页
     */
    public function search()
    {
        $this->display();
    }

    /**
     * 搜索商品
     */
    public function getProduct()
    {
        $page = (int)I('p') ? (int)I('p') : 0;
        $list = 10;
        $kw = trim(I('kw'));
        $where = " isUp=1 and isDelete=2 ";
        if (!empty($kw) && 'null' != $kw && null != $kw) {
            $where .= " and name like '%{$kw}%'";
        }
        $productList = M('product')->field("id,name,price,img")->where($where)->limit($page*$list, $list)->order(" id desc")->select();
        $this->ajaxSuccess($productList);
    }

    /**
     * 获取单个分类下商品列表
     */
    public function getCateGoods()
    {
        $page = (int)I('p') ? (int)I('p') : 0;
        $list = 10;
        $cateId = (int)I('get.cateId');
        if (empty($cateId))$this->ajaxError("参数错误：未知的分类id！");
        $cateInfo = M('category')->find($cateId);
        if (empty($cateInfo))$this->ajaxError("分类不存在！");
        $goodsList = M("product")
            ->where(array(
                "categoryId"=>$cateId,
                "isDelete"=>2,
                "isUp"=>1
            ))
            ->limit($page*$list, $list)
            ->select();
        $this->ajaxSuccess($goodsList);
    }

    //获取商品信息
    public function getProductInfo(){
        $productId = I('get.id');
        $pdt = M("product")
            ->where(array(
                "id"=>$productId,
                "isDelete"=>2,
                "isUp"=>1
            ))
            ->find();
        if ($pdt) {
            if ($pdt['imgs']) {
                $pdt['img_arr'] = explode(',',$pdt['imgs']);
            } else {
                $pdt['img_arr'][0] = $pdt['img'];
            }

        } else {
            $this->ajaxError("商品不存在或已下架！");
        }
        return $this->ajaxSuccess(array('product'=>$pdt));
    }

    //加入购物车
    public function addToCart()
    {
        $productId =  (int)I("id");
        $cartnum = (int)I('cartnum');
        $product = M("product")
            ->where(array("id"=>$productId))
            ->find();

        if(empty($product) || $product["isdelete"] == 1 || $product["isup"] == 2){
            $this->ajaxError("该商品不存在或未上架！");
        }
        if ($cartnum <= 0) {
            $this->ajaxError("数量不能为0！");
        }

        $cartList = parent::get("cart");

        $productId = $product["id"];
        if (array_key_exists($productId, $cartList)) {
            //添加过，数量+1
            $cartList[$productId]['num'] += $cartnum;
        } else {
            //新商品
            $newProduct = array($productId => array(
                'id' => $product["id"],
                'name' => $product["name"],
                'img' => $product['img'],
                'price' => $product['price'],
                'introduce' => $product['introduce'],
                'num' => $cartnum
            ));
            foreach ($newProduct as $key => $value) {
                $cartList[$key] = $value;
            }
        }

        parent::set("cart",$cartList);

        return $this->ajaxSuccess($productId);
    }
    
}